package br.indt.framework.ui.common;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import br.indt.framework.ui.ImageLoader;
import br.indt.framework.utils.StringUtils;

public abstract class TextField extends Widget{

	protected static final String LEFT_SIDE_DEFAULT_IMAGE = "/textfield/textfield_default_left_slice.png";
	protected static final String MIDDLE_DEFAULT_IMAGE = "/textfield/textfield_default_middle_slice.png";
	protected static final String RIGHT_SIDE_DEFAULT_IMAGE = "/textfield/textfield_default_right_slice.png";

	protected static final String LEFT_SIDE_SELECTED_IMAGE = "/textfield/textfield_selected_left_slice.png";
	protected static final String MIDDLE_SELECTED_IMAGE = "/textfield/textfield_selected_middle_slice.png";
	protected static final String RIGHT_SIDE_SELECTED_IMAGE = "/textfield/textfield_selected_right_slice.png";
	
	protected Label labelHeading = null;
	protected Label labelTxt = null;
	protected int maxSize = 0;
	protected int constraints = javax.microedition.lcdui.TextField.ANY;

	protected Image leftSideDefaultImage = null;
	protected Image middleDefaultImage = null;
	protected Image rightSideDefaultImage = null;

	protected Image leftSideSelectedImage = null;
	protected Image middleSelectedImage = null;
	protected Image rightSideSelectedImage = null;
	
	protected Image textFieldNormal = null;
	protected Image textFieldSelected = null;
	
	protected FocusListener focusListener = null;
	
	protected TextFieldListener textFieldListener = null;

	protected TextFieldUpdateListener textFieldUpdateListener = null;
	
	
	protected String titleSytemTextBox = "Text";
	
	protected String markup = null;
	

	

	public TextField(String name, String title, String text, int maxSize, int constraints, int minHeight) {
		super(name);
		labelHeading = new Label("txtLabel2", title);
		labelHeading.setMargin(0, 0, 0, 5);
		labelHeading.disable();
		labelTxt = new Label("txtLabel", text, false, 1, null);
		setImages();
		int margin =  ((textFieldNormal.getHeight() - labelTxt.getFont().getHeight()) / 2);
		labelTxt.setMargin(12, margin, 8, 5);
		labelTxt.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
		
		this.maxSize = maxSize;
		this.constraints = constraints;
		this.markup = null;
	}
	
	public Label getLabelHeading() {
		return labelHeading;
	}

	public void setWidth(int width) {
		labelHeading.setWidth(width - (leftMargin + rightMargin));
		labelTxt.setWidth(labelHeading.getWidth());
		this.width = width;
		calculateHeight();
	}
	
	protected void calculateHeight() {
		height = topMargin + bottomMargin + textFieldNormal.getHeight();
		if(!labelHeading.getText().equals(StringUtils.EMPTY)){
			height += labelHeading.getHeight();//getLabelTxtHeight() + labelHeading.getHeight();
		}
	}

	public void setText(String text) {
		labelTxt.setText(text);
		calculateHeight();
	}
	
	protected void verifyMarkupText(){
		if ((markup!= null)&&(markup.length()>0)){
			if (labelTxt.getText()!=null){
				if(labelTxt.getText().length()==0){
					labelTxt.setText(markup);
				}
			}else{
				labelTxt.setText(markup);
			}
		}
	}
	
	protected void clearMarkupText(){
		if ((markup!= null)&&(markup.length()>0)){
			if (labelTxt.getText()!=null){
				if(labelTxt.getText().equals(markup)){
					labelTxt.setText(StringUtils.EMPTY);
				}
			}
		}
	}
	public String getText() {
		clearMarkupText();
		return labelTxt.getText();
	}
	
	public String getTitle() {
		return labelHeading.getText();
	}
	
	public void setTitle(String title) {
		labelHeading.setText(title);
	}
	
	public abstract void paint(Graphics g, int x, int y, int maxWidth, int maxHeight);
	
	protected void setBackgroundTextFieldOnSelection(Graphics g, int x, int y, boolean selected) {
		if (selected) {

			this.setBackgroundTextField(g, x, y, this.textFieldSelected, 0x606060);
			if (this.focusListener != null){
				this.focusListener.focusGained(this);
			}

		} else {

			this.setBackgroundTextField(g, x, y, this.textFieldNormal, 0x9b9b9b);
			if (this.focusListener != null){
				this.focusListener.focusLost(this);
			}
		}
	}
	
	protected void setBackgroundTextField(Graphics g, int x, int y, Image image, int colorLabel){
		try {
	
			labelTxt.setTextColor(colorLabel);
			g.drawImage(image, x, y, Graphics.TOP | Graphics.LEFT);
		
		} catch (Exception e) {
			System.out.println("TextField::setBackgroundTextField " + e);
		}
	}
	
	public abstract void click(int x, int y);

	public abstract void pressFire();
	
	public void addFocusListener(FocusListener listener){
		this.focusListener = listener;
	}
	
	public void addTextFieldUpdateListener(TextFieldUpdateListener listener){
		this.textFieldUpdateListener = listener;
	}
	
	public void addTextFieldListener(TextFieldListener textFieldListener) {
		this.textFieldListener = textFieldListener;
	}

	protected void setImages(){
		textFieldNormal = ImageLoader.load(ImageLoader.TEXTFIELD_A);
		textFieldSelected = ImageLoader.load(ImageLoader.TEXTFIELD_B);
		
//		leftSideDefaultImage = ResourceManager.getImageResource(LEFT_SIDE_DEFAULT_IMAGE);
//		middleDefaultImage = ResourceManager.getImageResource(MIDDLE_DEFAULT_IMAGE);
//		rightSideDefaultImage = ResourceManager.getImageResource(RIGHT_SIDE_DEFAULT_IMAGE);
//		
//		leftSideSelectedImage = ResourceManager.getImageResource(LEFT_SIDE_SELECTED_IMAGE);
//		middleSelectedImage = ResourceManager.getImageResource(MIDDLE_SELECTED_IMAGE);
//		rightSideSelectedImage = ResourceManager.getImageResource(RIGHT_SIDE_SELECTED_IMAGE);
		
	}
	
	public String getTitleSytemTextBox() {
		return titleSytemTextBox;
	}

	public void setTitleSytemTextBox(String titleSytemTextBox) {
		this.titleSytemTextBox = titleSytemTextBox;
	}
	
	public String getMarkup() {
		return markup;
	}

	public void setMarkup(String markup) {
		this.markup = markup;
	}

	public int getMaxSize() {
		return maxSize;
	}

	public int getConstraints() {
		return constraints;
	}
	
}
